<?php
/**
 * Created by PhpStorm.
 * User: kang
 * Date: 16/2/25
 * Time: 14:32
 */
class Model_Auth extends Model_Base
{
    /**
     * 获取所有需要登录后权限认证的接口
     * @param array $idList
     * @return bool
     */
    public function node(array $idList = array())
    {
        try {
            $node = DB::select ()->from ( 'rbac_node' );
            !empty($idList) && $node->where('id','in',$idList);
            $result = $node->execute()->as_array();
            return $result;
        }catch(Exception $e){
            Log::instance()->add(Log::ERROR,$e->getMessage());
            return false;
        }
    }

    /**
     * 根据角色(组)id获取旗下可以访问的接口
     * @param $role
     * @return bool
     */
    public function roleNode($role)
    {
        try{
            return DB::select("node_id")->from('rbac_role_node')->where('role_id','=',$role)->execute()->as_array();
        }catch(Exception $e){
            Log::instance()->add(Log::ERROR,$e->getMessage());
            return false;
        }
    }

    /**
     * 更新角色权限
     * @param $role
     * @param array $node
     * @return bool
     * @throws Kohana_Exception
     */
    public function updateRoleNode($role, array $node)
    {
        try {
            Database::instance ()->begin ();
            DB::delete ( "rbac_role_node" )->where ( "role_id" , '=' , $role )->execute ();
            $insert = DB::insert ( "rbac_role_node" , array ( 'role_id' , 'node_id' ) );
            foreach ($node as $nid) {
                $insert = $insert->values ( array ( $role , $nid ) );
            }
            $insert->execute ();
            Database::instance ()->commit ();
            return true;
        }catch(Exception $e){
            Database::instance()->rollback();
            Log::instance()->add(Log::ERROR,$e->getMessage());
            return false;
        }
    }

    /**
     * 显示所有的角色
     * @return bool
     */
    public function roleShow()
    {
        try {
            return DB::select ()->from ( 'rbac_role' )->execute ()->as_array ();
        }catch(Exception $e){
            Log::instance()->add(Log::ERROR,$e->getMessage());
            return false;
        }
    }


    /**
     * 添加角色
     * @param $param['name']    角色名称
     * @return bool
     */
    public function roleAdd($param)
    {
        try {
            $result = DB::insert ( 'rbac_role' , array_keys ( $param ) )->values ( $param )->execute ();
            return isset($result[0]) ? $result[0]:false;
        }catch(Exception $e){
            Log::instance()->add(Log::ERROR,$e->getMessage());
            return false;
        }
    }

    /**
     * 增加节点
     * @param $param['name']    节点名称
     * @param $param['path']    节点路径
     * @param $param['pid']     节点父节点
     * @return bool
     */
    public function nodeAdd($param)
    {
        try{
            $result = DB::insert('rbac_node', array_keys($param))->values($param)->execute();
            return isset($result[0]) ? $result[0]:false;
        }catch(Exception $e){
            Log::instance()->add(Log::ERROR,$e->getMessage());
            return false;
        }
    }

    /**
     * 删除节点
     * @param $role
     * @return bool
     */
    public function roleDelete($role)
    {
        try {
            DB::delete ( 'rbac_role' )->where ( 'id' , '=' , $role )->execute ();
            return true;
        }catch(Exception $e){
            Log::instance()->add(Log::ERROR,$e->getMessage());
            return false;
        }
    }








}